//
//  EditView.m
//  Daybook
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "EditView.h"
#import "Common.h"

@interface EditView ()
<
 UIActionSheetDelegate,
 UIImagePickerControllerDelegate,
 UINavigationControllerDelegate,
 UITextViewDelegate
>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) UIView *keyboardView;
@property (nonatomic, strong) UILabel *placeholderLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *promptLabel;
@end


@implementation EditView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)setImage:(UIImage *)image {
    if (_image != image) {
        _image = image;
        [_photoButton setImage:_image forState:UIControlStateNormal];
    }
    
}
- (void)setRemarks:(NSString *)remarks {
    if (_remarks != remarks) {
        _remarks = remarks;
        _textView.text = _remarks;
        if (_textView.text.length != 0) {
            _placeholderLabel.hidden = YES;
        }
        
        
    }
    
}
- (void)createView {
        
    self.backgroundColor = [UIColor whiteColor];
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT / 15)];
    [self addSubview:_topView];
    //_topView.backgroundColor = [UIColor lightGrayColor];
    
    //时间
    
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(13 / 375.0 * MAIN_WIDTH, 0, 200 / 414.0 * MAIN_WIDTH, MAIN_HEIGHT / 15)];
    [_topView addSubview:_timeLabel];
    _timeLabel.textColor = [UIColor colorWithWhite:0.4 alpha:1];
    //解析时间戳
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [ dat timeIntervalSince1970] * 1000;
    CGFloat time = (CGFloat)a;
    NSString * timeStampString = [NSString stringWithFormat:@"%f", time];
    NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"yyyy年MM月dd日 HH:mm:ss.SSS"];
    
    NSString *mString = [objDateformat stringFromDate: date];
    NSString *str = [mString substringToIndex:11];
    _timeLabel.text = [NSString stringWithFormat:@"%@", str];
    _timeLabel.font = [UIFont systemFontOfSize:16];
    
    //输入备注
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10 / 414.0 * MAIN_WIDTH, MAIN_HEIGHT / 15, 355.0 / 375.0 * MAIN_WIDTH, MAIN_HEIGHT / 3 / 4 * 3)];
    [self addSubview:_textView];
    _textView.font = [UIFont systemFontOfSize:18 / 414.0  * MAIN_WIDTH];
    [_textView becomeFirstResponder];
    
    
    //    _textView.layer.borderWidth = 1;
    //    _textView.layer.borderColor = [UIColor grayColor].CGColor;
    
    self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 / 414.0 * MAIN_WIDTH + 7 / 414.0 * MAIN_WIDTH, MAIN_HEIGHT / 15 - 6 / 736.0 * MAIN_HEIGHT, 300 / 414.0 * MAIN_WIDTH, 50 / 736.0 * MAIN_HEIGHT)];
    [self addSubview:_placeholderLabel];
    _placeholderLabel.text = @"记录花销更记录生活...";
    _placeholderLabel.font = [UIFont systemFontOfSize:18 / 414.0 * MAIN_WIDTH];
    _placeholderLabel.textColor = [UIColor colorWithWhite:0.9 alpha:1];
    _textView.clearsContextBeforeDrawing = YES;
    _textView.keyboardType = UIKeyboardTypeNamePhonePad;
    
    self.keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, 100 / 736.0 * MAIN_HEIGHT)];
    _textView.delegate = self;
    _textView.inputAccessoryView = _keyboardView;
    
    
    self.photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _photoButton.frame = CGRectMake(MAIN_WIDTH / 20, _keyboardView.frame.size.height / 4, _keyboardView.frame.size.height / 2, _keyboardView.frame.size.height / 2);
    //_photoButton.backgroundColor = [UIColor redColor];
    [_keyboardView addSubview:_photoButton];
    [_photoButton setImage:[UIImage imageNamed:@"zhaoxiang"] forState:UIControlStateNormal];
    [_photoButton addTarget:self action:@selector(photoButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _photoButton.layer.cornerRadius = _keyboardView.frame.size.height / 2 / 2;
    _photoButton.layer.masksToBounds = YES;
    
    
    self.promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(MAIN_WIDTH - MAIN_WIDTH / 50 - _keyboardView.frame.size.height / 6 * 5 - 40 / 414.0 * MAIN_WIDTH, _keyboardView.frame.size.height / 12, _keyboardView.frame.size.height / 6 * 5 + 20 / 414.0 * MAIN_WIDTH, _keyboardView.frame.size.height / 6 * 5)];
    [_keyboardView addSubview:_promptLabel];
    _promptLabel.text = @"0 / 40";
    _promptLabel.textAlignment = NSTextAlignmentRight;
    _promptLabel.textColor = [UIColor grayColor];
    //    _keyboardView.layer.borderWidth = 1;
    //    _keyboardView.layer.borderColor = [UIColor grayColor].CGColor;
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, 1)];
    _lineView.backgroundColor = LINE_COLOR;
    [self addSubview:_lineView];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (![text isEqualToString:@""]) {
    
    _placeholderLabel.hidden = YES;
    
    }
    
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) {
        
        _placeholderLabel.hidden = NO;
        
    }
    
    return YES;
    
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length != 0) {
        _placeholderLabel.hidden = YES;
    }
    else {
        _placeholderLabel.hidden = NO;
    }
    NSInteger count = textView.text.length;
    _promptLabel.text = [NSString stringWithFormat:@"%ld / 40", count];
    //该判断用于联想输入
    if (textView.text.length > 40) {
        _promptLabel.textColor = [UIColor redColor];
        
    }
    else {
        _promptLabel.textColor = [UIColor grayColor];
    }
}

- (void)photoButtonAction:(UIButton *)button {
    [_textView resignFirstResponder];
    [self.delegate click];
}


@end
